package controllers;


import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.SolutionSubmission;
import models.RatingRegion;
import models.Location;


import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;


	public class BestOfRegionConnect{
		
	int iROWS;
	Boolean hasEntries;
	public List<HashMap<String, Object>> getQuery(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String, Object>>();
		Objectify ofy1 = ObjectifyService.begin();
		
		try {
			
			Query<RatingRegion> q = ofy1.query(RatingRegion.class).filter(fieldName, Value).filter("isActive", true);
			for (RatingRegion f1 : q.fetch()) {
				HashMap<String, Object> itemMap = new HashMap<String, Object>();				
				itemMap.put("solID", f1.getSolID());
				itemMap.put("Quarter", f1.getBestOfRegionQuarter());
				long l = f1.getSolID();			
				
							
					Objectify ofy2 = ObjectifyService.begin();
					Query<SolutionSubmission> a = ofy2.query(SolutionSubmission.class).filter("solID",l);
					for (SolutionSubmission f2 : a.fetch()) {	
						itemMap.put("solCode", f2.getSolCode()); //ADDED BY KULPRAPAS SEP 29,2012 - to get solution code
						itemMap.put("Title", f2.getTitle());
						itemMap.put("Location", f2.getLocation());
						itemMap.put("Region", f2.getLocationRegionName());
						itemMap.put("Customer", f2.getCustomer());
						itemMap.put("BenefitResult", f2.getAchievedTypeDesc());
						itemMap.put("Achieved", f2.getAnnProdSavings());
						itemMap.put("Site", f2.getProcessArea());
						
						
						hasEntries = true;
						objList.add(itemMap);
					
				
			
				}
					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return objList;
	}
	
	public List<HashMap<String, Object>> getAllRatingRegion() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String, Object>>();
		Objectify ofy1 = ObjectifyService.begin();
		
		try {
			
			Query<RatingRegion> q = ofy1.query(RatingRegion.class).filter("isActive", true);
			for (RatingRegion f1 : q.fetch()) {
				HashMap<String, Object> itemMap = new HashMap<String, Object>();				
				itemMap.put("solID", f1.getSolID());
				itemMap.put("Quarter", f1.getBestOfRegionQuarter());
				itemMap.put("Year", f1.getBestOfRegionYear());	
				itemMap.put("RateBY", f1.getRatedBY());
				itemMap.put("BestOfRegionON", f1.getBestOfRegionON());	
				
			
				hasEntries = true;						
				objList.add(itemMap);
					}
			
				
					
		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return objList;
	}

	

}

		
